/*
 * Copyright 2013 Walter Lütgenau.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package offset.nodes.client.dialog.navigation.model;

import java.applet.Applet;
import java.util.HashMap;
import netscape.javascript.JSObject;
import offset.nodes.client.dialog.navigation.view.AbstractCardPanel;
import offset.nodes.client.dialog.navigation.view.HeadingPanel;

/**
 *
 * @author Walter Lütgenau
 */
public class HeadingModel extends NavigationModel implements NodeSelectionListener {

    public static final String ID_CONTENT = "content";
    Applet applet;
    HeadingPanel panel;
    HashMap<String, Integer> headings = new HashMap<String, Integer>();

    public HeadingModel() {
        headings.put("h1", new Integer(1));
        headings.put("h2", new Integer(2));
        headings.put("h3", new Integer(3));
        headings.put("h4", new Integer(4));
        headings.put("h5", new Integer(5));
        headings.put("h6", new Integer(6));
    }

    @Override
    public void initPanel() {
        this.panel = new HeadingPanel();
        this.panel.setNodeSelectionListener(this);
    }

    @Override
    public void setApplet(Applet applet) {
        this.applet = applet;
    }

    @Override
    public void invalidate() {
        super.invalidate();
        panel.start();
        panel.end();
    }

    /**
     * Update tree after displaying a new repository node
     */
    @Override
    public void updatePage() throws Exception {
        JSNode window = new JSNode((JSObject) JSObject.getWindow(applet));
        JSDocument document = new JSDocument(window.getNode("parent.contentFrame.document"));
        JSNode content = document.getElementById(ID_CONTENT);
        if (content == null)
            return;

        panel.start();
        addHeadings(content);
        panel.end();
    }

    public AbstractCardPanel getPanel() {
        return panel;
    }

    /**
     * Add headings for the node and/or subnodes.
     *
     * @param node the node
     */
    protected void addHeadings(JSNode node) {
        String name = node.getName();
        if (headings.containsKey(name)) {
            panel.addHeading(node, node.getText(), headings.get(node.getName().toLowerCase()).intValue());
            return;
        }

        if (!name.equals(JSElement.E_DIV))
            return;

        for (int i = 0; i < node.getElementCount(); i++) {
            addHeadings((JSNode) node.getElement(i));
        }
    }

    @Override
    public void nodeSelected(String path) {
        JSNode node = (JSNode) panel.getSelectedUserObject();
        if (node != null) {
            JSNode window = new JSNode((JSObject) JSObject.getWindow(applet));
            window.getObject().call("highlight", new Object[] {node.getObject()});
            /*JSElement element = new JSElement(node);
            element.scrollIntoView();*/
        }
    }
}
